#include "FFmpegDemo.h"
#include <csignal>
#include <atomic>

std::atomic<bool> running(true);

void signalHandler(int signum) {
    if (signum == SIGINT) {
        running = false;
    }
}

int main(int argc, char* argv[]) {
    // 注册信号处理
    std::signal(SIGINT, signalHandler);
    if (argc < 2) {
        std::cerr << "Usage: " << argv[0] << " <video_file>" << std::endl;
        return 1;
    }

    FFmpegDemo wrapper;
    std::string video_file = argv[1];

    // 打开视频文件
    if (!wrapper.open(video_file)) {
        std::cerr << "Failed to open video file: " << video_file << std::endl;
        return 1;
    }

    std::cout << "Video opened successfully!" << std::endl;
    std::cout << "Resolution: " << wrapper.getWidth() << "x" << wrapper.getHeight() << std::endl;

    int frame_count = 0;
    const int frame_delay = 40;  // 每帧之间的延迟（毫秒）

    // 处理视频帧
    while (running && wrapper.isRunning()) {
        auto frame = wrapper.getFrame();
        if (frame.frame == nullptr) {
            std::cout << "End of video or error occurred" << std::endl;
            break;
        }

        std::cout << "Processing frame " << frame_count 
                  << ", PTS: " << frame.pts 
                  << std::endl;

        // 模拟帧处理
        std::this_thread::sleep_for(std::chrono::milliseconds(frame_delay));
        frame_count++;
    }

    // 关闭视频
    wrapper.close();
    std::cout << "Video processing stopped. Total frames processed: " << frame_count << std::endl;

    return 0;
}
